package com.zut.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zut.dto.SetmealDto;
import com.zut.mapper.SetmealMapper;
import com.zut.pojo.Setmeal;
import com.zut.pojo.SetmealDish;
import com.zut.service.SetmealDishService;
import com.zut.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Autowired
    private SetmealDishService setmealDishService;

    /*
    * 添加套餐
    *
    * */

    @Override
    public void saveWithDish(SetmealDto setmealDto) {

        //添加套餐信息到setmeal套餐表
        super.save(setmealDto);

        //添加套餐中的菜品信息到setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((setmealDish) -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);


    }
}
